﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using FMSpaceShooter.Screens;

namespace FMSpaceShooter.GameObjects
{
    abstract class ObjectBase : IDisposable
    {
        
        protected Vector2 position;
        protected readonly Texture2D texture;
        protected readonly Vector2 textureSize, origin;
        protected Vector2 scale;
        protected GameplayScreen screen;
        protected float rotation = 0, radius;
        protected int hp = 0;
        protected string textureName;

        public Vector2 Position { get { return position; } }
        public float Rotation { get { return rotation; } }
        public Vector2 TextureSize { get { return textureSize; } }
        public Color[] TextureData { get { return screen.GOM.GetTextureData(textureName); } }
        public virtual bool IsFriendly { get { return true; } }
        public virtual bool CanDamageEnemy { get { return false; } }
        public virtual bool isItem { get { return false; } }
        public virtual bool DoesCollide { get { return true; } }
        public virtual int Damage { get { return 0; } }
        public virtual bool isBoss { get { return false; } }

        protected Vector2 Scale
        {
            get { return scale; }
            set { scale = value;
            radius = (origin * scale).Length();
            }
        }

        public float Radius
        {
            get { return radius; }
        }

        public virtual void Hit(int damage)
        {
            hp -= damage;
            if (hp < 0)
            {
                hp = 0;
            }
            if (hp == 0)
            {
                Die();
            }
        }

        public virtual void Die()
        {
            screen.GOM.DisposeOf(this);
        }

        public ObjectBase(GameplayScreen screen, string textureName, Vector2? size = null)
        {
            this.screen = screen;
            this.textureName = textureName;
            this.texture = screen.GOM.GetTexture(textureName);
            textureSize = Util.Tools.GetTextureVector(texture);
            origin = textureSize / 2;

            if (size == null)
            {
                Scale = Vector2.One;
            }
            else
            {
                Scale = (Vector2)size / textureSize;
            }

        }

        public virtual void Update(GameTime gameTime)
        {

        }

        public virtual void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;

            spriteBatch.Draw(texture, position, null, Color.White,
                rotation, origin, scale, SpriteEffects.None, 0);
        }

        //public virtual RenderTarget2D DrawSolid(GameTime gameTime)
        //{
        //    var resolution = new Vector2(screen.GraphicsDeviceManager.PreferredBackBufferWidth,
        //        screen.GraphicsDeviceManager.PreferredBackBufferHeight);

        //    RenderTarget2D tempRT = new RenderTarget2D(screen.ScreenManager.GraphicsDevice, (int)resolution.X, (int)resolution.Y);

        //    screen.GraphicsDeviceManager.GraphicsDevice.SetRenderTarget(tempRT);

        //    SpriteBatch spriteBatch = new SpriteBatch(screen.ScreenManager.GraphicsDevice);
        //    spriteBatch.Begin();
        //    spriteBatch.Draw(texture, position, null, Color.White,
        //        rotation, origin, scale, SpriteEffects.None, 0);
        //    spriteBatch.End();

        //    screen.GraphicsDeviceManager.GraphicsDevice.SetRenderTarget(null);

        //    return tempRT;
        //}

        public virtual Matrix GetTransforms()
        {
            return Matrix.CreateTranslation(new Vector3(-origin, 0.0f)) *
                    Matrix.CreateScale(new Vector3(scale, 1f)) *
                    Matrix.CreateRotationZ(rotation) *
                    Matrix.CreateTranslation(new Vector3(position, 0.0f));
        }

        public void Dispose()
        {
        }
    }
}
